/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aic.server;

import aic.misc.DebugPrint;
import aic.security.ServerPasswordCallbackHandler;
import aic.ws.*;
import java.util.HashMap;
import java.util.Map;
import javax.xml.ws.Endpoint;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;
import aic.rest.CustomerManagementService;
import aic.rest.RestWrapperServiceImpl;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;

public class Server {

    public Server() throws Exception {

        boolean useSecurity = true;

        // START SNIPPET: publish //
        DebugPrint.println("Starting Server..");

        CreateDataServiceImpl createDataImplementor = new CreateDataServiceImpl();
        String createDataServiceAddress = "http://localhost:8081/CreateDataService";
        Endpoint.publish(createDataServiceAddress, createDataImplementor);

        DebugPrint.println("created createDataService");

        SMSServiceImpl smsServiceImplementor = new SMSServiceImpl();
        String smsServiceAddress = "http://localhost:8088/SMSService";
        Endpoint.publish(smsServiceAddress, smsServiceImplementor) ;

        DebugPrint.println("created SMSService");

        MailServiceImpl mailServiceImplementor = new MailServiceImpl();
        String mailServiceAddress = "http://localhost:8089/MailService";
        Endpoint.publish(mailServiceAddress, mailServiceImplementor);

        DebugPrint.println("created MailService");

        OrderManagementServiceImpl orderManagementServiceImplementor = new OrderManagementServiceImpl();
        String orderManagementAddress = "http://localhost:8083/OrderManagementService";
        Endpoint.publish(orderManagementAddress, orderManagementServiceImplementor);

        DebugPrint.println("created OrderManagementService");


        BankingServiceImpl bankingServiceImplementor = new BankingServiceImpl();
        String bankingServiceAddress = "http://localhost:8084/BankingService";

        if (!useSecurity) {
            Endpoint.publish(bankingServiceAddress, bankingServiceImplementor);
        } else {

            EndpointImpl jaxWsEndpoint = (EndpointImpl) Endpoint.publish(bankingServiceAddress, bankingServiceImplementor);
            org.apache.cxf.endpoint.Endpoint cxfEndpoint = jaxWsEndpoint.getServer().getEndpoint();

            DebugPrint.println("Creating the inProps..");
            Map<String, Object> inProps = new HashMap<String, Object>();

            DebugPrint.println("Creating wssIn..");
            WSS4JInInterceptor wssIn = new WSS4JInInterceptor(inProps);
            DebugPrint.println("Adding wssIn..");
            cxfEndpoint.getInInterceptors().add(wssIn);
//        cxfEndpoint.getInInterceptors().add(new SAAJInInterceptor()); // 2.0.x only; not needed in 2.1+

            DebugPrint.println("Creating the outProps..");
            Map<String, Object> outProps = new HashMap<String, Object>();

            WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
            cxfEndpoint.getOutInterceptors().add(wssOut);
//        cxfEndpoint.getOutInterceptors().add(new SAAJOutInterceptor()); // 2.0.x only; not needed in 2.1+

            DebugPrint.println("Putting something into the inProps..");
            inProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
            // Password type : plain text
            inProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
            // for hashed password use:
            //properties.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST);
            // Callback used to retrieve password for given user.
            inProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, ServerPasswordCallbackHandler.class.getName());//ServerPasswordHandler.class.getName());
        }

        DebugPrint.println("created BankingService");

        RestWrapperServiceImpl restWrapperServiceImplementor = new RestWrapperServiceImpl();
        String restWrapperServiceAddress = "http://localhost:8086/RestWrapperService";
        Endpoint.publish(restWrapperServiceAddress, restWrapperServiceImplementor);

        DebugPrint.println("created RestWrapperService");


        JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
        sf.setResourceClasses(CustomerManagementService.class);
        sf.setAddress("http://localhost:8085/");
        sf.create();
        // END SNIPPET: publish //


    }
}

